using System;
using System.Text;
using TccAg.VO;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace TccAg.DA
{
    //Todo: Criar método para retornar os cursos por período
    public class CursoDA
    {
        private String queryInsert = "INSERT INTO curso( NOME, SIGLA, SEMESTRES) VALUES( @nome, @sigla, @semestres)";
        private String queryUpdate = "UPDATE curso SET nome = @nome, sigla = @sigla, semestres = @semestres WHERE ID = @id";
        private String queryDelete = "DELETE FROM curso WHERE ID = @id";
        private String queryLoadAll = "SELECT  id, nome, sigla, semestres FROM curso";
        private String queryLoadByPk = "SELECT  id, nome, sigla, semestres FROM curso WHERE ID = @id";
        private String queryLoadByFk = "SELECT  id, nome, sigla, semestres FROM curso WHERE ";
        private SqlConnection conexao = new SqlConnection("Pegar conexão");

        //INSERIR 
        public int inserir(CursoVO curso)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryInsert;

                if (curso.Nome != null)
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) curso.Nome));
                else
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) DBNull.Value));

                if (curso.Sigla != null)
                        cmd.Parameters.Add(new SqlParameter("@sigla", (object) curso.Sigla));
                else
                        cmd.Parameters.Add(new SqlParameter("@sigla", (object) DBNull.Value));
                
                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //ATUALIZAR 
        public int atualizar(CursoVO curso)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                if (curso.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)curso.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryUpdate;

                if (curso.Nome != null)
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) curso.Nome));
                else
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) DBNull.Value));
                if (curso.Sigla != null)
                        cmd.Parameters.Add(new SqlParameter("@sigla", (object) curso.Sigla));
                else
                        cmd.Parameters.Add(new SqlParameter("@sigla", (object) DBNull.Value));
                
                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //EXCLUIR 
        public int excluir(CursoVO curso)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryDelete;

                if (curso.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)curso.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD ALL 
        public List<CursoVO> loadAll()
        {
            try
            {
                List<CursoVO> objRetorno = new List<CursoVO>();
                CursoVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objTemp = new CursoVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                           objTemp.Nome = (String)dr["nome"];

                        if (dr["sigla"] != DBNull.Value)
                           objTemp.Sigla = (String)dr["sigla"];


                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY PK - Passa um objeto do tipo VO para em casos onde usar chave primária composta ter essas propriedades preenchidas
        public CursoVO loadByPk(CursoVO curso)
        {
            try
            {
                CursoVO objRetorno = null;
                CursoVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (curso.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)curso.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new CursoVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                           objTemp.Nome = (String)dr["nome"];

                        if (dr["sigla"] != DBNull.Value)
                           objTemp.Sigla = (String)dr["sigla"];


                        objRetorno = objTemp;
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY FK - Passa um objeto do tipo VO para em casos onde usar chave estrangeira composta ter essas propriedades preenchidas
        public List<CursoVO> loadByFk(CursoVO curso)
        {
            try
            {
                List<CursoVO> objRetorno = new List<CursoVO>();
                CursoVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByFk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new CursoVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                           objTemp.Nome = (String)dr["nome"];

                        if (dr["sigla"] != DBNull.Value)
                           objTemp.Sigla = (String)dr["sigla"];



                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //DATATABLE LOAD ALL 
        public DataTable dataTableLoadAll()
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                SqlDataAdapter dtAdpt = new SqlDataAdapter(cmd);
                DataTable dtTbl = new DataTable("tab_curso");

                dtAdpt.Fill(dtTbl);
                return dtTbl;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

    }
}
